<?php


 function get_wigets_meta($wId=0,$meta_key=''){
     if( $meta_key != '' )
          $str= "select * from wigets_wigetsmeta where wiget_id  ='{$wId}' and meta_key='{$meta_key}' order by wmeta_id desc";
     else
          $str= "select * from wigets_wigetsmeta where wiget_id  ='{$wId}' order by wmeta_id desc ";

     $db = $GLOBALS['context']->db;

     $result = $db->getAll($str);

     return $result;

 }

function parse_theme_url($theme_part){

    $theme_part=trim($theme_part);

    return $GLOBALS['context']->get_app_conf('base_url')."theme/$theme_part";

}

function get_theme_directory(){
    return   ROOT_PATH.APP_PATH.DS.'views'.DS;
}

 function get_Widgets($wgt_id=0){
        if( empty($wgt_id) || !isset($wgt_id) ) return false;
        $request = & $GLOBALS['context']->request;
        $response = & $GLOBALS['context']->response;
        $app = & $GLOBALS['context']->app;
        $db = & $GLOBALS['context']->db;
        
        $app_action = $GLOBALS['context']->action;
		$template =  $GLOBALS['context']->theme; //添加模版显示widgets
		//echo $template;
        //if( ! isset($app['action'])  || empty($app['action']) ) return false;
        if(strpos($app_action, '/') === 0) $app_action = substr($app_action ,1);
        if( empty($app_action) ) $app_action = 'common';
		$result = $db->create_mapper('widget_opt wo')
					 ->join('widgets_widgetsmeta ww', 'wo.id=ww.widget_id', 'wmeta_id,meta_key,meta_value')
					 //->where(array('app_action'=>$app_action,'ww.meta_key'=>'widget_parent','ww.meta_value'=>$wgt_id))
					 ->where("app_action ='{$app_action}' or app_action ='common'")
					 ->where(array('ww.meta_key'=>'widget_parent','ww.meta_value'=>$wgt_id, 'template'=>$template))
					 ->order('wo.wgt_pos', false)
					 ->find_all_by();	 	
         if( ! $result || count( $result ) <=0 ) return false;
         foreach($result as $v){
                //echo'<div id="widgets'.$v['id'].'" wgID="'.$v['widget_id'].'">';
                echo'<div id="widgets'.$v['id'].'" wgID="'.$v['widget_id'].'">';
                render_widget($v['widget_id']);
                echo '</div>';
         }
 }
function list_widgets_meta($folder="",$type="border"){
    if ( empty($folder) ) {
		return false;
    }
    $files = list_files($folder);

    $i=0;
    $arrTemp = array();
    foreach($files as $file){
        if(file_exists( $file ) && is_file( $file) ){
            $template_data = implode( '', file( $file ) );
		    if ( preg_match( '|Widget '.$type.':(.*)$|mi', $template_data, $name )){
                     $s = explode('||', $name [1]);
                     $arrTemp[$i]['name'] =strtolower(trim($s[0]));
                     $arrTemp[$i]['action'] =strtolower(trim($s[1]));
            }
        }
        $i++;
    }
    return ($arrTemp);
}
 function list_files( $folder = '', $levels = 100 ) {
	if ( empty($folder) )
		return false;

	if ( ! $levels )
		return false;

	$files = array();
	if ( $dir = @opendir( $folder ) ) {
		while (($file = readdir( $dir ) ) !== false ) {
			if ( in_array($file, array('.', '..','.svn') ) )
				continue;
			if ( is_dir( $folder . '/' . $file ) ) {
				$files2 = list_files( $folder . '/' . $file, $levels - 1);
				if ( $files2 )
					$files = array_merge($files, $files2 );
				else
					$files[] = $folder . '/' . $file . '/';
			} else {
				$files[] = $folder . '/' . $file;
			}
		}
	}
	@closedir( $dir );
	return $files;
}

/*
 function list_theme_files( $folder = '', $levels = 100 ) {
	if ( empty($folder) )
		return false;

	if ( ! $levels )
		return false;

	$files = array();
	if ( $dir = @opendir( $folder ) ) {
		while (($file = readdir( $dir ) ) !== false ) {
			if ( in_array($file, array('.', '..','.svn') ) )
				continue;
			if ( is_dir( $folder . '/' . $file ) ) {
				$files2 = list_theme_files( $folder . '/' . $file, $levels - 1);
				if ( $files2 )
					$files = array_merge($files, $files2 );
				else
					$files[$folder][] = $folder . '/' . $file . '/';
			} else {
				$files[$folder][] = $folder . '/' . $file;
			}
		}
	}
	@closedir( $dir );
	return $files;
}

 */

function list_theme_files($theme){

     if ( empty($theme) ) return false;

     $files = list_files($theme,100);

     if ( empty($files) ||count($files) <=0 ) return false;

    $i = 0;$arrTemp = array();

    foreach($files as $file){
        $template_data = implode( '', file( $file ) );
         if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name )){
              $sTemp1 = explode('||', $name [1]);
              $arrTemp[$i]['name'] = trim($sTemp1[0]);
              $arrTemp[$i]['action'] = trim($sTemp1[1]);
              $arrTemp[$i]['file'] = str_replace($theme.'/', '', $file);
              $arrTemp[$i]['file'] = str_replace('.tpl.php', '', $arrTemp[$i]['file']);
              $i++;
         }
    }

    return $arrTemp;
}


function search_theme_directories($wgt_theme_directories) {

	//global $wgt_theme_directories;
	if ( empty( $wgt_theme_directories ) )
		return false;

	$theme_files = array();
	$wgt_broken_themes = array();


	foreach ( (array) $wgt_theme_directories as $theme_root ) {
		$theme_loc = $theme_root;


		//if ( '/' != WGT_CONTENT_DIR )
			//$theme_loc = str_replace(WGT_CONTENT_DIR, '', $theme_root);


		$themes_dir = @ opendir($theme_root);

		if ( !$themes_dir )
			return false;

		while ( ($theme_dir = readdir($themes_dir)) !== false ) {
			if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
				if ( $theme_dir[0] == '.' || $theme_dir == 'CVS' )
					continue;

				$stylish_dir = @opendir($theme_root . '/' . $theme_dir);
				$found_stylesheet = false;

				while ( ($theme_file = readdir($stylish_dir)) !== false ) {
					if ( $theme_file == 'style.css' ) {
						$theme_files[$theme_dir] = array( 'theme_file' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root );
						$found_stylesheet = true;
						break;
					}
				}
				@closedir($stylish_dir);

				if ( !$found_stylesheet ) { // look for themes in that dir
					$subdir = "$theme_root/$theme_dir";
					$subdir_name = $theme_dir;
					$theme_subdirs = @opendir( $subdir );

					$found_subdir_themes = false;
					while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
						if ( is_dir( $subdir . '/' . $theme_subdir) && is_readable($subdir . '/' . $theme_subdir) ) {
							if ( $theme_subdir[0] == '.' || $theme_subdir == 'CVS' )
								continue;

							$stylish_dir = @opendir($subdir . '/' . $theme_subdir);
							$found_stylesheet = false;

							while ( ($theme_file = readdir($stylish_dir)) !== false ) {
								if ( $theme_file == 'style.css' ) {
									$theme_files["$theme_dir/$theme_subdir"] = array( 'theme_file' => $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' => $theme_root );
									$found_stylesheet = true;
									$found_subdir_themes = true;
									break;
								}
							}
							@closedir($stylish_dir);
						}
					}
					@closedir($theme_subdirs);
					if ( !$found_subdir_themes )
						$wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' =>'Stylesheet is missing.');
				}
			}
		}
		@closedir( $themes_dir );
	}
	return $theme_files;
}



?>
